package com.qing.dish;

import java.util.Arrays;
import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class Demo4 {


	private static List<Dish> menu= Arrays.asList(
			new Dish("黄焖鸡米饭",false,15,"肉"),
			new Dish("水煮鱼",true,12,"鱼"),
			new Dish("酸菜鱼",false,20,"鱼"),
			new Dish("脆皮鸡米饭",false,18,"米饭"),
			new Dish("麻辣香锅",true,32,"米饭"),
			new Dish("鸡公煲",false,20,"肉"),
			new Dish("汉堡王",true,100,"米饭"),
			new Dish("至尊牛排",false,20,"肉"),
			new Dish("蛋炒饭",true,12,"素"),
			new Dish("咖喱",false,29,"米饭"),
			new Dish("炒白菜",false,8,"素"),
			new Dish("炒萝卜",false,9,"素"),
			new Dish("烤肉",true,50,"肉")
	);
	public static void main(String[] args) {

			 menu.stream().collect(Collectors.maxBy(Comparator.comparingInt(Dish::getCalories)));
			 menu.stream().collect(Collectors.minBy(Comparator.comparingInt(Dish::getCalories)));


		menu.stream().collect(Collectors.averagingInt(Dish::getCalories));

		IntSummaryStatistics summaryStatistics=menu.stream().collect(Collectors.summarizingInt(Dish::getCalories));
		summaryStatistics.getSum();
		summaryStatistics.getAverage();
		summaryStatistics.getMax();
		summaryStatistics.getMin();
		summaryStatistics.getCount();

		//黄焖鸡米饭,水煮鱼,酸菜鱼,脆皮鸡米饭,麻辣香锅,鸡公煲,汉堡王,至尊牛排,蛋炒饭,咖喱,炒白菜,炒萝卜,烤肉
		String menus=menu.stream().map(Dish::getName).collect(Collectors.joining(","));
		System.out.println(menus);

		menu.stream().collect(Collectors.reducing(0,Dish::getCalories,(i,j)->i+j));


		//根据类型分组
		menu.stream().collect(Collectors.groupingBy(Dish::getType));
		//类型 并大于20的分组
		menu.stream().collect(Collectors.groupingBy(Dish::getType,Collectors.mapping(dish -> dish.getCalories()>20,Collectors.toList())));

		menu.stream().collect(
				Collectors.groupingBy(
						Dish::getType,
						Collectors.collectingAndThen(
								Collectors.maxBy(
								Comparator.comparingInt(Dish::getCalories)),
								Optional::get
						)
				)
		).forEach((key,value)->{
			System.out.println(key+":==>"+value);
		});



		menu.stream().collect(Collectors.groupingBy(Dish::getType,Collectors.summarizingInt(Dish::getCalories)));

		menu.stream().collect(Collectors.groupingBy(Dish::getType,
				Collectors.mapping(dish -> {
					if(dish.getCalories()<15){
						return "很便宜";
					}else if(dish.getCalories()>15&&dish.getCalories()<=20){
						return "还吃的起";
					}else{
						return "土豪套餐";
					}
				},Collectors.toList())
		));





	}
}
